package com.ubis.ibms.plugin.composites.task;

import java.util.List;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

import com.ubis.ibms.plugin.common.constants.Constants;
import com.ubis.ibms.plugin.common.viewer.ComboLabelProvider;
import com.ubis.ibms.plugin.common.viewer.TableContentProvider;
import com.ubis.ibms.plugin.common.viewer.TableLabelProvider;
import com.ubis.ibms.plugin.composites.company.CompanyInfoComposite;
import com.ubis.ibms.plugin.dao.CompanyManager;
import com.ubis.ibms.plugin.dao.EstateManager;
import com.ubis.ibms.plugin.dialogs.TaskAddDialog;
import com.ubis.ibms.plugin.model.Building;
import com.ubis.ibms.plugin.model.Company;
import com.ubis.ibms.plugin.model.Floor;
import com.ubis.ibms.plugin.model.Room;

/**
 * 业主查询
 * @author diamond-wang
 *
 */
public class TaskQueryComposite extends Composite {
	
	//工作名称
	private Text nameText;
	//部门
	private ComboViewer depComboViewer;
	private Combo depCombo;
	//查询
	private Button queryButton;
	//清空
	private Button clearButton;
	//新增
	private Button addButton;
	
	private Table table;
	private TableViewer tableViewer;

	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public TaskQueryComposite(Composite parent, int style) {
		super(parent, style);
		
		GridLayout layout = new GridLayout(1, false);
		layout.marginWidth = 10;
		layout.marginHeight = 10;
		setLayout(layout);
		
		Composite titleComposite = new Composite(this, SWT.NONE);
		titleComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		titleComposite.setLayout(new GridLayout(2, false));
		
		Label title = new Label(titleComposite, SWT.NONE);
		title.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.BOLD));
		title.setText("工作查询");
		
		addButton = new Button(titleComposite, SWT.NONE);
		addButton.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				// 点击添加时
				TaskAddDialog addDialog = new TaskAddDialog(TaskQueryComposite.this.getShell());
				addDialog.open();
			}
		});
		addButton.setText("新增");
		
		Composite queryComposite = new Composite(this, SWT.BORDER);
		queryComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		queryComposite.setLayout(new GridLayout(5, false));

		//工作名称
		Label nameLabel1 = new Label(queryComposite, SWT.RIGHT);
		nameLabel1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		nameLabel1.setText("名称：");
		nameText = new Text(queryComposite, SWT.BORDER);
		nameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
				
		//部门
		Label depLabel = new Label(queryComposite, SWT.RIGHT);
		depLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		depLabel.setText("部门：");
		depComboViewer = new ComboViewer(queryComposite, SWT.READ_ONLY);
		depCombo = depComboViewer.getCombo();
		depCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));
		depComboViewer.setContentProvider(ArrayContentProvider
				.getInstance());
		depComboViewer.setLabelProvider(new ComboLabelProvider());
		
		//点击查询按钮的动作
		queryButton = new Button(queryComposite, SWT.NONE);
		queryButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				System.out.println("queryButton mouse up.");
			}
		});
		queryButton.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, false, 1, 1));
		queryButton.setText("查询");
		
		//点击清空按钮的动作
		clearButton = new Button(queryComposite, SWT.NONE);
		clearButton.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				clearInput();//清空输入
			}
		});
		clearButton.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, false, 1, 1));
		clearButton.setText("清空");
		
		tableViewer = new TableViewer(this, SWT.BORDER | SWT.FULL_SELECTION);
		table = tableViewer.getTable();
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		TableViewerColumn VtaskIdColumn = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn taskIdColumn = VtaskIdColumn.getColumn();
		taskIdColumn.setResizable(false);
		taskIdColumn.setAlignment(SWT.CENTER);
		taskIdColumn.setWidth(150);
		taskIdColumn.setText("工作编号");
		
		TableViewerColumn VtaskNameColumn = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn taskNameColumn = VtaskNameColumn.getColumn();
		taskNameColumn.setResizable(false);
		taskNameColumn.setAlignment(SWT.CENTER);
		taskNameColumn.setWidth(150);
		taskNameColumn.setText("工作名称");
		
		TableViewerColumn VdepColumn = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn depColumn = VdepColumn.getColumn();
		depColumn.setResizable(false);
		depColumn.setAlignment(SWT.CENTER);
		depColumn.setWidth(150);
		depColumn.setText("所属部门");
		
		TableViewerColumn VdesColumn = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn desColumn = VdesColumn.getColumn();
		desColumn.setResizable(false);
		desColumn.setAlignment(SWT.CENTER);
		desColumn.setWidth(150);
		desColumn.setText("工作描述");
		
		TableViewerColumn VassessColumn = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn assessColumn = VassessColumn.getColumn();
		assessColumn.setResizable(false);
		assessColumn.setAlignment(SWT.CENTER);
		assessColumn.setWidth(150);
		assessColumn.setText("考核标准");
		
        tableViewer.setContentProvider(new TableContentProvider());
		tableViewer.setLabelProvider(new TableLabelProvider());
		tableViewer.setInput(null);
		//双击某条记录时，进入该条记录的信息视图
		tableViewer.addDoubleClickListener(new IDoubleClickListener(){
			public void doubleClick(DoubleClickEvent event) {
				//返回选中行的company
				IStructuredSelection selection = (IStructuredSelection)tableViewer.getSelection();
			}
			
		});
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

	/**
	 * 清空输入
	 */
	public void clearInput(){
		depComboViewer.setSelection(null);
		nameText.setText("");
		tableViewer.setInput(null);
	}
}
